package com.leetcodehot.problems;

public class problems2487 {
    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    public ListNode reverseList(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode nxt = cur.next;
            cur.next = pre;
            pre = cur;
            cur = nxt;
        }
        return pre;
    }

    /**
     * 一：反转链表
     * 二：max记录，保持递增
     * 三：再反转一次 输出
     */
    public ListNode removeNodes(ListNode head) {
        head = reverseList(head);
        ListNode dummy = new ListNode(-1, head);
        ListNode cur = dummy;
        int max = -1;
        while (cur.next != null) {
            if (cur.next.val < max) {
                //移除
                cur.next = cur.next.next;
            } else {
                max = cur.next.val;
                cur = cur.next;
            }
        }
        return reverseList(dummy.next);
    }
}
